//
//  THCustomSegment.m
//  PiaoYou
//
//  Created by lst on 15/4/3.
//  Copyright (c) 2015年 lst. All rights reserved.
//

#import "SampuxCustomSegment.h"
#import "UIView+Extension.h"

#define RGB(r,g,b,opaque) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:opaque]

@interface SampuxCustomSegment()<UIAlertViewDelegate>
@property (weak, nonatomic) IBOutlet UIButton *segOneBtn;
@property (weak, nonatomic) IBOutlet UIButton *segTwoBtn;
@property (weak, nonatomic) IBOutlet UIImageView *backImageView;
@property (strong,nonatomic) NSMutableArray *btnArray ;
@property (strong,nonatomic) UILabel *alertLab ;
@property (strong,nonatomic) UIAlertView *alertView ;

- (void)moveBackImageViewToLeft;
- (void)moveBackImageViewToRight;

@end
@implementation SampuxCustomSegment

- (void)awakeFromNib
{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(msgSynchronization:) name:NOTIFICATION_RESPONSE_MSG_RECEIVE_MODE object:nil];
    
    _segOneBtn.hitTestEdgeInsets = UIEdgeInsetsMake(- 10 , -10, -10, 0);
    _segTwoBtn.hitTestEdgeInsets = UIEdgeInsetsMake(- 10 , 0, -10, -10);

}

- (UILabel *)alertLab
{
    if (_alertLab == nil) {
        _alertLab = [[UILabel alloc]initWithFrame:CGRectMake(20, kScreenHeight * 0.3, kScreenWidth - 40, 200)];
        _alertLab.text = @"设备数据将实时同步给手机，流量可能会增大";
        _alertLab.font = THFont(12);
        [_alertLab setCorner:5];
        _alertLab.backgroundColor = THWhiteAlplaColor(0.2);
    }
    return _alertLab;
}

- (UIAlertView *)alertView
{
    if (_alertView == nil) {
        _alertView = [[UIAlertView alloc]initWithTitle:nil message:@"设备数据将实时同步给手机，流量可能会增大" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        _alertView.delegate = self;
    }
    return _alertView;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    [self layoutIfNeeded];
    self.btnArray = [[NSMutableArray alloc] init];
    self.width = Sampux6WScale(60);
    self.height = (28);
    CGFloat radius = self.height/2 ;
    
    [self setCorner:radius];
    [self.backImageView setCorner:radius];
    [self.btnArray addObject:self.segOneBtn];
    [self.btnArray addObject:self.segTwoBtn];
}

- (void)setConfigModel:(SPDeviceConfigModel *)configModel
{
    _configModel = configModel;
    if (_configModel.isDataSyn == 1) {
//        self.selectLeft = NO;
        self.backImageView.x = self.width - self.backImageView.width ;
    }else
    {
//        self.selectLeft = YES;
        self.backImageView.x = 0;
    }
}

- (IBAction)clickSegmentButtonWithTag:(UIButton *)button {
    NSLog(@"%ld",(long)button.tag);
    if (self.configModel != nil) {
        if (button.tag == 0) {
            self.selectLeft = YES;
            
            [GBInterMaster_CommForSystem requestMsgReceiveMode:0 withDeviceAccount:self.configModel.deviceAccount  withMsgMode:0];
            
        }else{
            self.selectLeft = NO;
            //        [SVProgressHUD showSuccessWithStatus:@"设备数据将实时同步给手机，流量可能会增大"];
            [SVProgressHUD showWithStatus:@"设备数据将实时同步给手机，流量可能会增大"];
            
            [GBInterMaster_CommForSystem requestMsgReceiveMode:0 withDeviceAccount:self.configModel.deviceAccount  withMsgMode:1];
            
            //        _alertView = [[UIAlertView alloc]initWithTitle:nil message:@"设备数据将实时同步给手机，流量可能会增大" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            //        _alertView.delegate = self;
            //        [_alertView show];
        }
    }
    
}

//左移
-(void)moveBackImageViewToLeft
{
    [UIView animateWithDuration:0.3 animations:^{
        self.backImageView.x = 0 ;
    } completion:^(BOOL finished) {
    }];
}

//右移
-(void)moveBackImageViewToRight
{
    [UIView animateWithDuration:0.3 animations:^{
        self.backImageView.x = self.width - self.backImageView.width ;
        
    } completion:^(BOOL finished) {
//        [MBProgressHUD showError:@"设备数据将实时同步给手机，流量可能会增大"];
    }];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"%ld",(long)buttonIndex);
    if (buttonIndex == 0) {
        [self moveBackImageViewToLeft];
        [GBInterMaster_CommForSystem requestMsgReceiveMode:0 withDeviceAccount:self.configModel.deviceAccount withMsgMode:0];
    }else
    {
        [self moveBackImageViewToRight];
                [GBInterMaster_CommForSystem requestMsgReceiveMode:0 withDeviceAccount:self.configModel.deviceAccount  withMsgMode:1];
    }
}

- (void)setSelectLeft:(BOOL)selectLeft
{
    _selectLeft = selectLeft;
    if (selectLeft) {
        [self moveBackImageViewToLeft];

    } else {
        [self moveBackImageViewToRight];
    }
    if ([self.delegate respondsToSelector:@selector(handleThingsWithSampuxCustomSegmentSelect:)]) {
        [self.delegate handleThingsWithSampuxCustomSegmentSelect:selectLeft];
    }
}

- (void)msgSynchronization:(NSNotification *)notification
{
    [NSThread sleepForTimeInterval:2];
    [SVProgressHUD dismiss];
//    [MBProgressHUD hideHUD];
    id userInfo = [notification userInfo];
    if(userInfo == nil){
        return;
    }
    NSDictionary * result = userInfo;
    int msgNumber = [[result objectForKey:@"msgNumber"] intValue];
    int responseCode = [[result objectForKey:@"responseCode"] intValue];
    NSLog(@"msgSynchronization == 结果，消息序号：%d，响应码：%x",msgNumber,responseCode);
    if(responseCode == 0x00) {
        
    } else {
        [SPResponseCodePrompt responseCodePrompt:responseCode];
        [SVProgressHUD showErrorWithStatus:@"设置同步失败"];
//        [MBProgressHUD showError:@"设置同步失败"];
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    
}

@end
